﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZG_member.Model;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using System.Data;
namespace ZG_member.DAL
{
   public class billTemplates
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id, string Name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from billTemplates");
            strSql.Append(" where id=@id and Name = @Name");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
                    new SqlParameter("@Name", SqlDbType.NVarChar,200)
};
            parameters[0].Value = id;
            parameters[1].Value = Name;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZG_member.Model.billTemplates model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update billTemplates set ");
            strSql.Append("Name=@Name,");
            strSql.Append("Templates=@Templates");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,1000),
                    new SqlParameter("@Templates", SqlDbType.NVarChar,1000),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Templates1;
            parameters[2].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ZG_member.Model.billTemplates model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into billTemplates(");
            strSql.Append("Name,Templates)");
            strSql.Append(" values (");
            strSql.Append("@Name,@Templates)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,1000),
                    new SqlParameter("@Templates", SqlDbType.NVarChar,1000)
                                         };
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Templates1;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        public ZG_member.Model.billTemplates GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,name,templates from billTemplates ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            ZG_member.Model.billTemplates model = new ZG_member.Model.billTemplates();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["name"] != null && ds.Tables[0].Rows[0]["name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["templates"] != null && ds.Tables[0].Rows[0]["templates"].ToString() != "")
                {
                    model.Templates1 = ds.Tables[0].Rows[0]["templates"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}
